package org.example._02_music;

import javafx.embed.swing.JFXPanel;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;

import javax.swing.*;
import java.awt.*;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class Mp3Player {
    private JFrame frame;
    private JLabel titleLabel;
    /**
     * 存储mp3 歌名
     */
    private JList<String> playlist;
    /**
     * 存储mp3 播放路径
     */
    private List<String> songList;
    /**
     * sun mp3 解析对象
     */
    private MediaPlayer mediaPlayer;

    public Mp3Player() {
        initUI();
        initMediaPlayer();
    }

    private void initUI() {
        // 初始化窗口
        frame = new JFrame("mp3-Player");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(800, 400);
        frame.setLayout(new BorderLayout());

        // 初始化控制面板
        JPanel controlPanel = new JPanel();
        controlPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10));
        frame.add(controlPanel, BorderLayout.NORTH);

        // 添加标题标签
        titleLabel = new JLabel("No Song Selected");
        controlPanel.add(titleLabel);

        // 添加上一曲按钮
        JButton previousButton = new JButton("Previous");
        previousButton.addActionListener(e -> {
            int selectedIndex = playlist.getSelectedIndex();
            if (selectedIndex > 0) {
                String previousSong = songList.get(selectedIndex - 1);
                playlist.setSelectedIndex(selectedIndex - 1);
                playSong(previousSong);
            }
        });
        controlPanel.add(previousButton);

        // 添加播放按钮
//        playButton = new JButton("Play");
        JButton playButton = new JButton("Play");
        playButton.addActionListener(e -> {
            String selectedSong = playlist.getSelectedValue();
            if (selectedSong != null) {
                playSong(selectedSong);
            }
        });
        controlPanel.add(playButton);

        // 添加停止按钮
        JButton stopButton = new JButton("Stop");
        stopButton.addActionListener(e -> {
            if (mediaPlayer != null && mediaPlayer.getStatus() == MediaPlayer.Status.PLAYING) {
                mediaPlayer.stop();
                titleLabel.setText("No Song Selected");
            }
        });
        controlPanel.add(stopButton);

        // 添加下一曲按钮
        JButton nextButton = new JButton("Next");
        nextButton.addActionListener(e -> {
            int selectedIndex = playlist.getSelectedIndex();
            if (selectedIndex < songList.size() - 1) {
                String nextSong = songList.get(selectedIndex + 1);
                playlist.setSelectedIndex(selectedIndex + 1);
                playSong(nextSong);
            }
        });
        controlPanel.add(nextButton);

        // 初始化播放列表
        songList = new ArrayList<>();
        playlist = new JList<>();
        playlist.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        playlist.addListSelectionListener(e -> {
            String selectedSong = playlist.getSelectedValue();
            if (selectedSong != null) {
                titleLabel.setText(selectedSong);
            }
        });
        frame.add(new JScrollPane(playlist), BorderLayout.CENTER);

        // 显示窗口
        frame.setVisible(true);
    }

    private void initMediaPlayer() {
        // 初始化JavaFX媒体播放器
        JFXPanel fxPanel = new JFXPanel();
        // 播放绑定。窗口绑定
        frame.add(fxPanel, BorderLayout.SOUTH);
    }

    private void playSong(String songPath) {
        // 使用JavaFX媒体播放器播放音乐
        if (mediaPlayer != null && mediaPlayer.getStatus() == MediaPlayer.Status.PLAYING) {
            mediaPlayer.stop();
        }
        /**
         * new File
         * 媒体库地址  mp3 地址
         */
        Media media = new Media(new File(songPath).toURI().toString());
        mediaPlayer = new MediaPlayer(media);
        mediaPlayer.play();

        // 更新标题
        titleLabel.setText(new File(songPath).getName());

        // 更新播放列表选择项
        int selectedIndex = playlist.getSelectedIndex();
        if (selectedIndex == -1) {
            playlist.setSelectedIndex(0);
        }
        else if (mediaPlayer != null) {
            mediaPlayer.setOnEndOfMedia(() -> {
                int nextIndex = selectedIndex + 1;
                if (nextIndex < songList.size()) {
                    String nextSong = songList.get(nextIndex);
                    playlist.setSelectedIndex(nextIndex);
                    playSong(nextSong);
                }
            });
        }
    }

    public void addSong(String songPath) {
        // 添加歌曲到播放列表
        songList.add(songPath);
        DefaultListModel<String> listModel = new DefaultListModel<>();
        for (String song : songList) {
            File file = new File(song);
            listModel.addElement(file.getName());
        }
        playlist.setModel(listModel);
    }

    public static void main(String[] args) {
        Mp3Player mp3Player = new Mp3Player();
        // 添加歌曲
        mp3Player.addSong("苏公堤.mp3");
        mp3Player.addSong("狐言.mp3");
        mp3Player.addSong("凤凰传奇-万神纪.mp3");
        mp3Player.addSong("凉凉.mp3");
    }
}
